package com.xiebishe.mylibrary.common.groupAdapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link RecyclerView.Adapter} subclass.
 * Created by summer on 16/9/29.
 */

public abstract class RecyclerAdapterWithData<DATA, VH extends RecyclerView.ViewHolder> extends RecyclerAdapter<VH> {
    protected List<DATA> mData;
    protected Context mContext;

    private OnItemClickListener<DATA> mOnItemClickListener;

    public RecyclerAdapterWithData(Context mContext) {
        this.mContext = mContext;
        this.mData = new ArrayList<>();
    }

    @NonNull
    @Override
    public final VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(mContext).inflate(getLayoutId(viewType), parent, false);
        final VH holder = onCreateVH(parent, v, viewType);
        if (mOnItemClickListener != null) {
            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getAdapterPosition();
                    position = getPosition(position);
                    if (position >= 0 && position < mData.size()) {
                        DATA data = mData.get(position);
                        mOnItemClickListener.onItemClick(data, position);
                    }
                }
            });
        }
        return holder;
    }

    @Override
    public final void onBindViewHolder(@NonNull VH holder, int position) {
        onBindVH(holder, getItem(position), position);
    }


    @Nullable
    public DATA getItem(int position) {
        if (position < getItemCount() && position >= 0) {
            return mData.get(position);
        } else {
            return null;
        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }


    public void update(List<DATA> newData) {
        if (newData == null) {
            mData.clear();
            notifyDataSetChanged();
        } else {
            resetData(newData);
            notifyDataSetChanged();
        }
    }

    private void resetData(List<DATA> newData) {
        mData.clear();
        mData.addAll(newData);
    }

    protected abstract @NonNull
    VH onCreateVH(@NonNull ViewGroup parent, @NonNull View v, int viewType);

    protected abstract void onBindVH(@NonNull VH holder, @NonNull DATA item, int position);

    protected abstract int getLayoutId(int viewType);

    public void setOnItemClickListener(OnItemClickListener<DATA> listener) {
        this.mOnItemClickListener = listener;
    }

    public void add(DATA bean) {
        mData.add(bean);
        notifyItemInserted(mData.size() - 1);
    }

    public void addAll(List<DATA> data) {
        int start = mData.size();
        mData.addAll(data);
        notifyItemRangeInserted(start, data.size());
    }

    public void removeAll() {
        mData.clear();
        notifyDataSetChanged();
    }

    public boolean remove(DATA data) {
        int index = mData.indexOf(data);
        boolean remove = false;
        if (index != -1) {
            remove = mData.remove(data);
            notifyItemRemoved(index);
        }
        return remove;
    }

    public DATA remove(int position) {
        DATA d = mData.remove(position);
        notifyItemRemoved(position);
        return d;
    }

    /**
     * 返回重新包装后的数据集合，防止adapter内部数据集外泄
     *
     * @return 包装后的数据集合
     */
    public List<DATA> getData() {
        return new ArrayList<>(mData);
    }

    public void add(DATA item, int position) {
        mData.add(position, item);
        notifyItemInserted(position);
    }

    public void update(DATA item, int position) {
        mData.set(position, item);
        notifyItemChanged(position);
    }

    public boolean isEmpty() {
        return mData == null || mData.size() == 0;
    }

    public interface OnItemClickListener<DATA> {
        void onItemClick(DATA item, int position);
    }
}
